<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			body { margin: 0; }
		</style>
		<script src="../utils/setTitle.js"></script>
	</head>
	<body>
		<script type="importmap">
			{
				"imports": {
					"three": "https://unpkg.com/three/build/three.module.js"
				}
			}
		</script>
		<script type="module">
			import * as THREE from 'three';
			const scene = new THREE.Scene();
			const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
			const geometry = new THREE.BoxGeometry( 1, 1, 1 );
			const material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
			const cube = new THREE.Mesh( geometry, material );
			scene.add( cube );
			const material2 = new THREE.LineBasicMaterial( { color: 0x0000ff  } );

			camera.position.z = 5;
			const renderer = new THREE.WebGLRenderer();
			renderer.setSize( window.innerWidth, window.innerHeight );
			document.body.appendChild( renderer.domElement );


			const raycaster = new THREE.Raycaster();
			const pointer = new THREE.Vector2();

			function onPointerMove( event ) {

				// 将鼠标位置归一化为设备坐标。x 和 y 方向的取值范围是 (-1 to +1)

				pointer.x = ( event.clientX / window.innerWidth ) * 2 - 1;
				pointer.y = - ( event.clientY / window.innerHeight ) * 2 + 1;

			}

			function animate() {
				requestAnimationFrame( animate );
				raycaster.setFromCamera( pointer, camera );
				
					raycaster.setFromCamera( pointer, camera );

				// 计算物体和射线的焦点
				const intersects = raycaster.intersectObjects( scene.children );
				cube.material.color.set( 0xffff00 );
				for ( let i = 0; i < intersects.length; i ++ ) {
					intersects[ i ].object.material.color.set( 0xff0000 );
				}
				cube.rotation.x += 0.01;
				cube.rotation.y += 0.01;
				renderer.setSize( window.innerWidth, window.innerHeight );
				renderer.render( scene, camera );
			}
			window.addEventListener( 'pointermove', onPointerMove );

			animate();
		</script>
	</body>
</html>